package com.cskaoyan.realm;

import com.cskaoyan.bean.User;
import com.cskaoyan.service.UserManagementService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author LiaoLong
 * @date 2021-05-14 17:42
 */
@Component
public class MobileRealm extends AuthorizingRealm {
    @Autowired
    private UserManagementService userManagementService;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        MobileToken mobileToken = (MobileToken) token;
        String phone = mobileToken.getUsername();
        int count = userManagementService.getUserByMobile(phone);
        if (count != 0) {
            throw new AuthenticationException("该手机号已注册");
        }
        return new SimpleAuthenticationInfo(phone, "default", this.getName());
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }
}
